package com.example.smart_city_0829.activity.youth_lnn;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.YouthInn.PolicyList;
import com.example.smart_city_0829.entity.YouthInn.TalentPolicyArea;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeLv;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class TalentActivity extends BaseActivity {

    private int id = -1;

    private ImageView im;
    private TextView introduce;
    private MaxSizeLv lv;

    private int pageNum = 1;
    private final int pageSize = 8;

    private PolicyListAdapter adapter;
    private List<PolicyList.DataBean> sortedData;
    private DateFormat format = SimpleDateFormat.getDateTimeInstance();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_talent;
    }

    @Override
    protected void initView() {
        im = (ImageView) findViewById(R.id.im);
        introduce = (TextView) findViewById(R.id.introduce);
        lv = (MaxSizeLv) findViewById(R.id.lv);
    }

    @Override
    protected void initData() {
        id = getIntent().getIntExtra("id", -1);

        api.getTalentAreaOfId(id).enqueue(new NetCallBack<TalentPolicyArea>() {
            @Override
            public void onResponse(Call<TalentPolicyArea> call, Response<TalentPolicyArea> response) {
                final TalentPolicyArea.DataBean data = response.body().getData();
                Glide.with(TalentActivity.this)
                        .load(Constant.BASE_URL + data.getImgUrl())
                        .into(im);
                introduce.setText(data.getIntroduce());
            }
        });

        api.getPolicyOfAreaId(id).enqueue(new NetCallBack<PolicyList>() {
            @Override
            public void onResponse(Call<PolicyList> call, Response<PolicyList> response) {
                 sortedData = response.body().getData()
                        .stream()
                        .sorted(Comparator.comparing(PolicyList.DataBean::getCreateTime))
                        .collect(Collectors.toList());

                if (adapter == null) {
                    adapter = new PolicyListAdapter(sortedData, TalentActivity.this);
                    lv.setAdapter(adapter);
                } else {
                    adapter.resetData(sortedData);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Intent intent = new Intent(TalentActivity.this, PolicyDetailActivity.class);
                intent.putExtra("id", sortedData.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "人才政策";
    }

    static class PolicyListAdapter extends CommonAdapter<PolicyList.DataBean, ViewHolder> {

        public PolicyListAdapter(List<PolicyList.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(PolicyList.DataBean data, int position, ViewHolder viewHolder, View convertView, Context context) {
            viewHolder.title.setText(data.getTitle());
            viewHolder.time.setText(data.getCreateTime());
        }

        @Override
        protected ViewHolder getViewHolder() {
            return new ViewHolder();
        }

        @Override
        protected void initView(ViewHolder viewHolder, View convertView) {
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.policy_list_item;
        }
    }

    static class ViewHolder {
        TextView title;
        TextView time;
    }
}